package services;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.aton.db.SessionFactory;

import models.BackCount;
import models.BackInfo;
import models.mappers.BackCountMapper;
import models.mappers.BackInfoMapper;
import models.mappers.RejectCountMapper;
import vos.BackCountVo;
import vos.CountMonthVo;
import vos.RejectCountSearchVo;
import vos.UpdateBackCountVo;

/**
 * 驳回返款Service类
 * 
 * @ClassName: RejectCountService 
 * @author: wangsm
 * @date: 2016年6月7日 上午9:14:39
 */
public class RejectCountService {
	private static final SimpleDateFormat dateTimeFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	/**
	 * 统计驳回记录总数
	 *
	 * @Title: count 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long count(RejectCountSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RejectCountMapper mapper = ss.getMapper(RejectCountMapper.class);
            return mapper.count(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取驳回
	 *
	 * @Title: getCount 
	 * @param vo
	 * @return
	 * @return: List<BackCountVo>
	 */
	public static List<BackCountVo> getReject(RejectCountSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			RejectCountMapper mapper = ss.getMapper(RejectCountMapper.class);
            return mapper.getReject(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 重新统计
	 *
	 * @Title: reCount 
	 * @param ids
	 * @return
	 * @return: boolean
	 */
	public static boolean reCount(List<CountMonthVo> ids,String operatorid) {
		// TODO Auto-generated method stub
       Collections.sort(ids, new Comparator<CountMonthVo>(){  
            /*  
             * int compare(CountMonthVo o1, CountMonthVo o2) 返回一个基本类型的整型，  
             * 返回负数表示：o1 小于o2，  
             * 返回0 表示：o1和o2相等，  
             * 返回正数表示：o1大于o2。  
             */  
            public int compare(CountMonthVo o1, CountMonthVo o2) { 
                return o1.cmonth.compareTo(o2.cmonth);
            }
        });
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			BackInfoMapper mapper = ss.getMapper(BackInfoMapper.class);
			List<UpdateBackCountVo> counts=new ArrayList<UpdateBackCountVo>();
			Map<String,Object> map=new HashMap<String,Object>();
			BackCountMapper bmapper=ss.getMapper(BackCountMapper.class);
			Date today=new Date();
			for(CountMonthVo count : ids){
				//获取该经销商上个月结束时间
				String startdate="";
				BackCount backcount=bmapper.getByShopMonth(count.shopid,count.cmonth);
				if(backcount!=null&&backcount.enddate!=null
						&&backcount.enddate.getTime()<dateTimeFormater.parse(count.cmonth+"-01 00:00").getTime()){
					startdate=dateTimeFormater.format(backcount.enddate);
				}
				else{
					startdate=count.cmonth+"-01 00:00";
				}
				//获取本月的最后一天作为结束日期
				String enddate=BackCountService.getMonthMaxDay(dateTimeFormater.parse(count.cmonth+"-01 00:00"));
				//如果当前日期小于月份最后一天则以当前时间作为结束日期
				if(dateTimeFormater.parse(enddate).getTime()>today.getTime()){
					enddate=dateTimeFormater.format(today);
				}
				UpdateBackCountVo vo=new UpdateBackCountVo(count.shopid,startdate,
						enddate,operatorid,new Date(),count.cmonth);
				counts.add(vo);
			} 
			map.put("lists", counts);
			map.put("modfifyname", operatorid);
			mapper.reCount(map);
			ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 对重新统计记录进行保存
	 *
	 * @Title: saveReject 
	 * @param ids 选中的记录
	 * @param operatorid
	 * @return
	 * @return: boolean
	 */
	public static boolean saveReject(List<Long> ids, String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			BackInfoMapper infomapper = ss.getMapper(BackInfoMapper.class);
			BackCountMapper countmapper = ss.getMapper(BackCountMapper.class);
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("lists", ids);
			List<BackInfo> backs=infomapper.getBackByIds(map);
			map=new HashMap<String,Object>();
			map.put("backs", backs);
			infomapper.updateReject(map);
			countmapper.updatePrice(map);
			ss.commit();
		}catch(Exception e){
			ss.rollback();
			e.printStackTrace();
			return false;
		}
		finally{
			ss.close();
		}
		return true;
	}
	
}
